package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.FundAccountDao;
import com.xbongbong.saas.domain.entity.FundAccountEntity;
import com.xbongbong.saas.model.FundAccountModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouwq
 * @date 2019/10/23 15:10
 */

@Service("fundAccountModel")
public class FundAccountModelImpl implements FundAccountModel {

    @Resource
    private FundAccountDao fundAccountDao;

    @Override
    public Integer insert(FundAccountEntity entity) throws XbbException {
        long now = DateTimeUtil.getLong();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        return fundAccountDao.insert(entity);
    }

    @Override
    public Integer update(FundAccountEntity entity) throws XbbException {
        long now = DateTimeUtil.getLong();
        entity.setUpdateTime(now);
        return fundAccountDao.update(entity);
    }

    @Override
    public List<FundAccountEntity> findEntitys(Map<String, Object> param){
        return fundAccountDao.findEntitys(param);
    }

    @Override
    public Integer deleteByKey(Long id) throws XbbException {
        FundAccountEntity fundAccountEntity = new FundAccountEntity();
        fundAccountEntity.setId(id);
        fundAccountEntity.setDel(DelEnum.DELETE.getDel());
        return update(fundAccountEntity);
    }

    @Override
    public Integer updateAmount(String corpid, Long id, Double amount) throws XbbException {
        return fundAccountDao.updateAmountByCorpidAndId(amount, corpid, id);
    }

    @Override
    public Integer updateBatchAmount(List<FundAccountEntity> list) throws XbbException {
        return fundAccountDao.updateBatchAmountByCorpidAndId(list);
    }

    @Override
    public FundAccountEntity getByIdCorpid(Long id, String corpid) {
        return fundAccountDao.getByIdCorpid(id, corpid);
    }

    @Override
    public List<Long> getIdsByCorpid(String corpid) {
        return fundAccountDao.getIdsByCorpid(corpid);
    }

    @Override
    public double sumBalanceByCorpid(String corpid) {
        return fundAccountDao.sumBalanceByCorpid(corpid);
    }

    @Override
    public double sumInitialAmountByCorpid(String corpid) {
        return fundAccountDao.sumInitialAmountByCorpid(corpid);
    }

    @Override
    public void initFundAccount(List<String> corpidList) {
        fundAccountDao.initFundAccount(corpidList);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return fundAccountDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer updateEnable(Long id, Integer enable) throws XbbException{
        FundAccountEntity fundAccountEntity = new FundAccountEntity();
        fundAccountEntity.setId(id);
        fundAccountEntity.setEnable(enable);
        return fundAccountDao.update(fundAccountEntity);
    }

    @Override
    public FundAccountEntity getDefaultFundAccount(String corpid) {
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put(StringConstant.CORPID, corpid);
        params.put(StringConstant.DEL, DelEnum.NORMAL);
        params.put("enable", BasicConstant.ONE);
        params.put("start", BasicConstant.ZERO);
        params.put("pageNum", BasicConstant.ONE);
        List<FundAccountEntity> list = findEntitys(params);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public Integer save(FundAccountEntity entity) throws XbbException {
        if (entity.getId() == null || entity.getId() == 0) {
            return insert(entity);
        }
        return update(entity);
    }
}
